<?php
namespace app\backstage\logic;
use think\Db;
class UserGrowthTrendReport
{
    public function getListBy30()
    {
        $field = 'create_date,new_user_num,jiazhuo_user_num';

        $type = 0;
        $where = [];
        $uid = request()->_user->id;

        if (request()->_role->alias == 'agent') {
            $type = 1;
            $where[] = ['agent_id','=',$uid];
        }

        if (request()->_role->alias == 'channel') {
            $type = 2;
            $where[] = ['channel_id','=',$uid];
        }

        if (request()->_role->alias == 'eb') {
            $type = 1;

            $Account = \think\facade\App::model('Account', 'logic');
            $agents = $Account->getAgents(request()->_user->id);
            $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

            $where[] = ['agent_id','in',$agent_id_gh];
        }

        $start_date = date('Y-m-d', strtotime('-29days'));
        $end_state = date('Y-m-d');
        $where[] = ['create_date','>=',$start_date];
        $where[] = ['create_date','<=',$end_state];

        $orderBy = 'create_date ASC';

        $UserGrowthTrendReport = \think\facade\App::model('UserGrowthTrendReport');
        $list = $UserGrowthTrendReport->getList($type, $where, $field, 1, 30, $orderBy);
        $list = array_column($list,null,'create_date');

        $dates = getDateByRange($start_date,$end_state);
        foreach ($dates as $k=>$v)
        {
            $new_user_num[] = isset($list[$v]) ? $list[$v]['new_user_num'] : 0;
            $jiazhuo_user_num[] = isset($list[$v]) ? $list[$v]['jiazhuo_user_num'] : 0;
        }


        return ['new_user_num'=>$new_user_num,'jiazhuo_user_num'=>$jiazhuo_user_num];
    }
}